<?php

namespace app\admin\controller;

use think\facade\View;
use app\admin\model\Admin as AdminModel;
use app\admin\model\Group as GroupModel;
use app\admin\validate\Admin as AdminV;

class Admin extends Base
{
    public function index()
    {
        $list = AdminModel::with(['group'])->order('id desc')->paginate([
            'list_rows'     =>  20,
            'query'         =>  $this->get
        ]);
        return View::fetch('', ['list' => $list]);
    }

    public function add()
    {
        if ($this->request->isAjax()) {
            try {
                $this->post['type'] = 1;
                validate(AdminV::class)->check($this->post);
                $this->post['password_true'] = $this->post['password'];
                $this->post['password'] = md5($this->post['password']);
                (new AdminModel)->replace()->save($this->post);
            } catch (\Exception $e) {
                return $this->error($e->getMessage() ?: '添加失败');
            }
            return $this->success('添加成功', 'index');
        }
        $group_list = GroupModel::where(['status' => 1])->select();
        return View::fetch('', ['group_list' => $group_list]);
    }

    public function edit()
    {
        $info = AdminModel::field('username,status,group_id,avatar')->find($this->get['id']);
        if ($this->request->isAjax()) {
            try {
                validate(AdminV::class)->scene('edit')->check($this->post);
                if(!empty($this->post['password'])){
                    $this->post['password_true'] = $this->post['password'];
                    $this->post['password'] = md5($this->post['password']);
                }
                $info->replace()->save($this->post);
            } catch (\Exception $e) {
                return $this->error($e->getMessage() ?: '修改失败');
            }
            return $this->success('修改成功', 'index');
        }
        $group_list = GroupModel::where(['status' => 1])->select();
        return View::fetch('', ['group_list' => $group_list, 'info' => $info]);
    }

    public function del()
    {
        if ($this->request->isAjax()) {
            $info = AdminModel::where($this->post)->find();
            try {
                AdminModel::destroy(function ($query) {
                    $query->where(['id' => $this->post['id']]);
                });
            } catch (\Exception $e) {
                return $this->error($e->getMessage() ?: '删除失败');
            }
            return $this->success('删除成功', 'index');
        }
    }
    public function upload()
    {
        if ($this->request->isAjax()) {
            try {
                $file = request()->file('image');
                // validate(['image' => 'filesize:2048000|fileExt:jpg,png,gif'])->check(['image' => $file]);
                $savename = \think\facade\Filesystem::putFile('topic', $file);
            } catch (\Exception $e) {
                return $this->error($e->getMessage() ?: '上传失败');
            }
            return $this->success('上传成功', '', '/upload/'.$savename);
        }
    }
}
